package com.car.job;

import java.util.Map;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.PostMethod;

import com.alibaba.fastjson.JSONObject;
import com.car.common.model.ImageQpulp;
import com.google.common.collect.Maps;
import com.utils.HttpRequestUtil;
import com.utils.PicUtil;


public class TheadImgCheck implements Runnable {

	private Map<String,Object> handleData = null;
	
	public TheadImgCheck(Map<String,Object> data) {
		this.handleData = data;
	}
	
	
	@Override
	public void run() {
		String [] imges = (String[]) handleData.get("img");
		ImageQpulp one = null;
		if (imges != null && imges.length > 0 ) {
			for(String str : imges) {
				one = new ImageQpulp();
				one.setRelationId(Integer.valueOf(String.valueOf(handleData.get("relation_id"))) );
				one.setType(Integer.valueOf(String.valueOf(handleData.get("type"))));
				one.setImg(str);
				one.setCreateTime(System.currentTimeMillis()/1000L);
				one.setUpdateTime(System.currentTimeMillis()/1000L);
				// {"code":0,"message":"","result":{"label":2,"score":0.89246,"review":false}}
				String result = HttpRequestUtil.sendGet(PicUtil.QINIU_DIR + str + "?qpulp");
				JSONObject json = JSONObject.parseObject(result);
				if (json != null && json.getInteger("code") == 0) {
					JSONObject jsonResult = json.getJSONObject("result");
					int label = jsonResult.getInteger("label");
					one.setLabel(label);
					int score = (int) (jsonResult.getDoubleValue("score") * 1000);
					one.setScore(score);
					one.setReview(jsonResult.getBoolean("review")? 1: 2);
					one.save();
					// pulp_score 100
					if (label == 0 && score >= 100) {
						String [] ids = new String[] { String.valueOf(one.getId()) };
						ImageQpulp.dao.unpass(ids, true);
					// sex_score 950	
					} else if (label == 1 && score >= 100) {
						String [] ids = new String[] { String.valueOf(one.getId()) };
						ImageQpulp.dao.unpass(ids, true);
					}
				} else {
					if (json == null) {
						one.setMessage("未知错误");
					} else {
						one.setMessage(json.getString("message"));
					}
					one.save();
				}
				
			}
			
		}
		if (one != null) {
			new PicUtil().handle_check_status(one);	
		}
	}
	
	public static void main(String[] args) {
		
		Map<String,Object> handleData = Maps.newHashMap(); 
		handleData.put("relation_id", 100320);
		handleData.put("img", "Fh0B1gkJ4F8zWskfGGTc_4J_0OEP");
		handleData.put("type", 2);
		new Thread(new TheadImgCheck(handleData)).start();
		/*
		try {
			HttpClient client = new HttpClient();
	    	client.getParams().setContentCharset("UTF-8");
	    	PostMethod postMethod = new PostMethod(PicUtil.QINIU_DIR + "java_admin_02e1f3a9c1674ac696076c1835d6a1cc.jpg");
	    	
	    	int status = client.executeMethod(postMethod);
			if (status == HttpStatus.SC_OK) {
				String result = postMethod.getResponseBodyAsString();
				System.out.println(result);
			} else {
				System.out.println("图片不存在");
			}
			//String result = HttpRequestUtil.sendGet(PicUtil.QINIU_DIR + "java_admin_02e1f3a9c1674ac696076c1835d6a1cc.jpg");
		} catch (Exception e) {
			System.out.println(1);
		}
		*/
	}
	
}
